package com.wlz.md5;

import com.wlz.file.FileCopyTest;
import org.apache.commons.codec.digest.DigestUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 *  文件 md5 比较
 * @author wlz
 * @date 2022-11-11  11:43 上午
 */
public class FileMd5CompareTest {

    public static void main(String[] args) throws IOException {
        String filePathA = "/Users/wlz/Desktop/home/upgrade--package-done-file/7783f152-576a-4c67-af85-6ca0d7b92a300.zip";
        String filePathB = copyChannel("/Users/wlz/Desktop/cd4984cd-8c9b-4b52-a90d-fc4f9cb6629b0-备份.zip",filePathA.substring(filePathA.lastIndexOf("/")));
        boolean b = md5Compare(filePathA, filePathB);
        System.out.println("result:" +b);
    }

    public static boolean md5Compare(String filePathA, String filePathB) throws IOException {
        String md5A = DigestUtils.md5Hex(new FileInputStream(filePathA));
        String md5B = DigestUtils.md5Hex(new FileInputStream(filePathB));
        System.out.println("md5A: " + md5A);
        System.out.println("md5B: " + md5B);
        return md5A.equals(md5B) ? true : false;
    }

    public static String copyChannel(String fullPath, String fileName) throws IOException {
        long time = System.currentTimeMillis();
        File originalPackageFile = new File(fullPath);
        String destPackagePath = fullPath.substring(0, fullPath.lastIndexOf("."));
        File destFilePath = new File(destPackagePath);
        if (!destFilePath.exists()) {
            destFilePath.mkdirs();
        }
        File destPackageFile = new File(destPackagePath, fileName);

        FileCopyTest.copyFileUsingFileStreams(originalPackageFile, destPackageFile);
        System.out.println("time : " + (System.currentTimeMillis() - time));
        return destPackageFile.getAbsolutePath();
    }
}
